package com.csust.travel.controller;

import com.csust.travel.entity.*;
import com.csust.travel.mapper.TransactionMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.log.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/transaction")
public class TransactionContraller {

    @Autowired
    TransactionMapper transactionMapper;

    @RequestMapping("/hascommented")
    public ResBean hasCommented(Integer num1) {
        transactionMapper.setHasCommented(num1);
        return new ResBean(true, "");
    }

    @RequestMapping("/handleComId")
    public ResBean handleComId(@RequestBody commentParams3 params3) {
        System.out.println(params3);
        Integer num1 = params3.getOrder_id();
        Integer num2 = params3.getComment_id();
        System.out.println(num1+"ppp");
        System.out.println(num2+"666");
        transactionMapper.setCommentId(num1,num2);
        return new ResBean(true, "");
    }

    @RequestMapping("/findBuy")
    public ResBean pageBuy(@RequestBody pageDate pageData) {
        PageHelper.startPage(pageData.getCurrentPage(), pageData.getPageSize());
        List<Transaction> Items;
        System.out.println("111 + "+pageData.getBuyer_id());
        Items = transactionMapper.findBuy(pageData.getBuyer_id());
        for (Transaction transaction : Items) {
            transaction.setBook_img(transactionMapper.findImgById(transaction.getBooks_id()));
            transaction.setBook_name(transactionMapper.findInfoByIdb2(transaction.getBooks_id()));
            transaction.setBuyer_name(transactionMapper.findName(transaction.getBuyer_id()));
            transaction.setSeller_name(transactionMapper.findName(transaction.getSeller_id()));
        }
        PageInfo<Transaction> commentPageInfo = new PageInfo<>(Items);
        return new ResBean(true, null, commentPageInfo);
    }


    @RequestMapping("/findNoReview")
    public ResBean pageNoReview(@RequestBody pageDate pageData) {
        PageHelper.startPage(pageData.getCurrentPage(), pageData.getPageSize());
        List<Transaction> Items;
        System.out.println("111 + "+pageData.getBuyer_id());
        Items = transactionMapper.findNoReview(pageData.getBuyer_id());
        for (Transaction transaction : Items) {
            transaction.setBook_img(transactionMapper.findImgById(transaction.getBooks_id()));
            transaction.setBook_name(transactionMapper.findInfoByIdb2(transaction.getBooks_id()));
            transaction.setBuyer_name(transactionMapper.findName(transaction.getBuyer_id()));
            transaction.setSeller_name(transactionMapper.findName(transaction.getSeller_id()));
        }
        PageInfo<Transaction> commentPageInfo = new PageInfo<>(Items);
        return new ResBean(true, null, commentPageInfo);
    }

    @RequestMapping("/findSell")
    public ResBean pageSell(@RequestBody pageDate pageData) {
        PageHelper.startPage(pageData.getCurrentPage(), pageData.getPageSize());
        List<Transaction> Items;
        System.out.println("1113 + "+pageData.getBuyer_id());
        System.out.println("11122 + "+pageData.getSeller_id());
        Items = transactionMapper.findSell(pageData.getSeller_id());
        for (Transaction transaction : Items) {
            transaction.setBook_img(transactionMapper.findImgById(transaction.getBooks_id()));
            transaction.setBook_name(transactionMapper.findInfoByIdb2(transaction.getBooks_id()));
            transaction.setBuyer_name(transactionMapper.findName(transaction.getBuyer_id()));
            transaction.setSeller_name(transactionMapper.findName(transaction.getSeller_id()));
        }
        PageInfo<Transaction> commentPageInfo = new PageInfo<>(Items);
        return new ResBean(true, null, commentPageInfo);
    }

    @RequestMapping("/page")
    public ResBean page(@RequestBody pageDate pagination){
        PageHelper.startPage(pagination.getCurrentPage(),pagination.getPageSize());
        List<Transaction> Items;
        if (pagination.getQueryString() == null || pagination.getQueryString().trim().equals("")){
            Items = transactionMapper.findAllItem();
            System.out.println(Items);
        }else {
            Items = transactionMapper.findByString(pagination.getQueryString());
        }
        for (Transaction transaction : Items) {
            transaction.setBook_name(transactionMapper.findInfoByIdb2(transaction.getBooks_id()));
        }
        for (Transaction transaction : Items) {
            transaction.setBook_img(transactionMapper.findImgById(transaction.getBooks_id()));
        }

        PageInfo<Transaction> transactionPageInfo = new PageInfo<>(Items);
        return new ResBean(true,null,transactionPageInfo);
    }
    @RequestMapping("/two")
    public ResBean findTwo(Integer id) {
        Transaction transaction =new Transaction();
        Integer book_id = transactionMapper.findBookId(id);
        transaction.setBook_img(transactionMapper.findImgById(book_id));
        return new ResBean(true,null,transaction);
    }


    @RequestMapping("/one")
    public ResBean findOne(Integer id) {
        PurchaseInformation purchaseInformation = new PurchaseInformation();
        try {
            Integer buyerId = transactionMapper.findBuyerId(id);
            Integer sellerId = transactionMapper.findSellerId(id);
            Integer bookId = transactionMapper.findBookId(id);
            purchaseInformation.setBuyer_id(buyerId);
            purchaseInformation.setBuyer_name(transactionMapper.findName(buyerId));
            purchaseInformation.setBuyer_college(transactionMapper.findCollege(buyerId));
            purchaseInformation.setBuyer_major(transactionMapper.findMajor(buyerId));
            purchaseInformation.setSeller_id(sellerId);
            purchaseInformation.setSeller_name(transactionMapper.findName(sellerId));
            purchaseInformation.setSeller_college(transactionMapper.findCollege(sellerId));
            purchaseInformation.setSeller_major(transactionMapper.findMajor(sellerId));
            purchaseInformation.setBooks_id(bookId);
            purchaseInformation.setBooks_name(transactionMapper.findInfoByIdb2(bookId));
            purchaseInformation.setBook_code(transactionMapper.findInfoByIdb1(bookId));
            purchaseInformation.setTransaction_date(transactionMapper.findTransaction_date(id));
            purchaseInformation.setAmount(transactionMapper.findAmount(id));
            purchaseInformation.setQuantity(transactionMapper.findQuantity(id));
            purchaseInformation.setId(id);

            return new ResBean(true, null, purchaseInformation);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResBean(false, null, null);
    }

    @RequestMapping("/del")
    public  ResBean delItem(Integer id){
        try {
            transactionMapper.delItem(id);
            return new ResBean(true,"删除成功666");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResBean(false,"删除失败");
    }


}
